﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ReportlossesModelCommand;
using B.S.BaseData.Manage.Domain.Entities;
using DAL;
using MediatR;
using Microsoft.Data.SqlClient;

namespace B.S.BaseData.Manage.Api.Read.Apilaction.CoomandHandler.ReportlossesModelCommandHandler
{
    /// <summary>
    /// 导出批量查询显示
    /// </summary>
    public class ReportlossesExportModelCommandHandler : IRequestHandler<ReportlossesExportModelCommand, ApiReqult<List<ReportlossesModel>>>
    {
        private readonly DBHelper bHelper;

        public ReportlossesExportModelCommandHandler(DBHelper bHelper)
        {
            this.bHelper = bHelper;
        }
        /// <summary>
        /// 导出批量查询显示
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiReqult<List<ReportlossesModel>>> Handle(ReportlossesExportModelCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiReqult<List<ReportlossesModel>>();

            try
            {
                string ids = string.Join(",", request.Id);
                string sql = $"SELECT * FROM ReportlossesModel WHERE Id IN ({ids}) AND IsDelete = 0 ORDER BY Reviewtime DESC;";
                List<ReportlossesModel>? hoModes = bHelper.GetList<ReportlossesModel>(sql);

                var managementModel = bHelper.GetList<ReportlossesModel>(sql);

                if (managementModel != null)
                {
                    result.Data = hoModes;
                    result.Msg = ApiEnum.查询成功.ToString();
                }
                else
                {
                    result.Msg = ApiEnum.查询失败.ToString();
                    result.Code = ApiEnum.查询失败;
                }

            }
            catch (SqlException ex)
            {
                result.Msg = ApiEnum.查询失败.ToString();
                result.Code = ApiEnum.查询失败;
            }

            return Task.FromResult(result);
        }
    }
}
